using System.Text.RegularExpressions;
using Cfg.Exceptions;

namespace Cfg.ValidationAttributes
{
    public class RegexMatch : CfgFinalizedStringValueValidatorBaseAttribute
    {
        public string Pattern { get; private set; }

        public RegexMatch(string pattern, params string[] contexts)
        {
            Contexts = contexts;
            Pattern = pattern;
        }

        protected override void Validate(string finalizedStringValue)
        {
            var regex = new Regex(Pattern);

            if (!regex.IsMatch(finalizedStringValue))
            {
                throw new ConfigException(string.Format("'{0}' did not match pattern '{1}'", finalizedStringValue, Pattern));
            }
        }
    }
}